import {ref,reactive} from 'vue'
import {onLoad,onReachBottom} from "@dcloudio/uni-app"
export const useList = (props)=>{
    let page = ref(1);
    let limit = ref(10)
    let isDone = ref(false)
    let dataList = ref([])
    let searchVal = ref('')
    let loading = ref(true)
    const doSearch = ()=>{
        page.value = 1;
        dataList.value = []
        isDone.value = false
        loadData({
            [props.searchName]:searchVal.value
        });
    }
    const loadData = (param)=>{
        props.listApi({
            page:page.value,
            limit:limit.value,
            ...param
        }).then(res=>{
            if(res.data&&res.data.length){
                dataList.value.push(...res.data)
                isDone.value = false;
                loading.value = false;
                console.log(props);
                props.callBack && props.callBack;
            }else{
                isDone.value = true
                setTimeout(() => {
                    loading.value = false;
                }, 1500);
            }
        })
    }
    onReachBottom(()=>{
        if(!isDone.value){
            page.value++;
            loadData(props.otherParam ? props.otherParam:'');
        }
    })
    return {
        dataList,
        loadData,
        page,
        doSearch,
        loading,
        searchVal
    }
}